package com.hgw.Demo04_CreateInterator.Exercises19_8_4;

import java.util.List;

/**
 * Data time:2021/12/18 13:59
 * StudentID:2019112118
 * Author:hgw
 * Description: 电视频道迭代器，充当具体迭代器
 */
public class TCListIterator implements AbstractTCListIterator {
    private List<TC> tcList;
    private int cursor1;        // 定义一个游标，用于记录正向遍历的位置
    private int cursor2;        // 定义一个游标，用于记录逆向遍历的位置

    public TCListIterator(TCList tcList) {
        this.tcList = tcList.getTcList();       // 获取集合对象
        cursor1 = 0;                            // 设置正向遍历游标的初始值
        cursor2 = this.tcList.size();           // 设置逆向遍历游标的初始值
    }

    @Override
    public void next() {
        if (cursor1 < tcList.size()) {
            cursor1++;
        }
    }

    @Override
    public boolean isLast() {
        return (cursor1==tcList.size());
    }

    @Override
    public void previous() {
        if (cursor2 > -1) {
            cursor2--;
        }
    }

    @Override
    public boolean isFirst() {
        return (cursor2==-1);
    }

    @Override
    public TC getNextItem() {
        return tcList.get(cursor1);
    }

    @Override
    public TC getPreviousItem() {
        return tcList.get(cursor2);
    }
}
